import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter)

import store from '@/store/index';

import Home from '../pages/Home/index.vue'
import Login from '@/pages/Login/index.vue'
import Register from '../pages/Register/index.vue'
import Search from '../pages/Search/index.vue'
import Detail from '@/pages/Detail/index.vue'
import AddCartSuccess from "@/pages/AddCartSuccess/index.vue"
import ShopCart from "@/pages/ShopCart/index.vue"
import Trade from '@/pages/Trade/index.vue'
import Pay from '@/pages/Pay/index.vue'
import PaySuccess from '@/pages/PaySuccess/index.vue'
import Center from '@/pages/Center/index.vue'
import myOrder from '@/pages/Center/myOrder'
import groupOrder from '@/pages/Center/groupOrder'

const router = new VueRouter({
    routes: [
        {
            path: "/Detail/:id",
            name: "Detail",
            component:Detail
        },
        {
            name:'Home',
            path: '/home',
            component: Home,
            meta: {
                showfooter: true,
                showList:true
            }
        },
        {
            name:'Login',
            path: '/login',
            component: Login,
            meta: {
                showfooter:false
            }
        },
        {
            name:'Register',
            path: '/register',
            component: Register,
            meta: {
                showfooter:false
            }
        },
        {
            name:'Search',
            path: '/search/:keyword?',
            component: Search,
            meta: {
                showfooter: true,
                showList: false
            }
        },
        {
            name: 'AddCartSuccess',
            path: '/AddCartSuccess',
            component: AddCartSuccess,
            meta: {
                showfooter:true
            }
        },
        {
            name: 'shopCart',
            path: '/shopCart',
            component:ShopCart
        },
        {
            name: 'trade',
            path: '/trade',
            component: Trade
        },
        {
            name: 'pay',
            path: '/pay',
            component:Pay
        },
        {
            name: 'paySuccess',
            path: '/paySuccess',
            component:PaySuccess
        },
        {
            name: 'center',
            path: '/center',
            component: Center,
            children: [
                {
                    name: 'myOrder',
                    path: 'myOrder',
                    component:myOrder
                },
                {
                    name: 'groupOrder',
                    path: 'groupOrder',
                    component:groupOrder
                },
                { //路由重定向不是无效，而是需要放到对应的路由组件当中去
                    path: '/center',
                    redirect:'/center/myOrder'
                }
            ]
        },
        //**************路由重定向，开始默认展示的路由组件****
        {
            path: '*',
            redirect:'/home'
        },
    ],
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部
        return { y: 0 }
      },
});
const routerPush = VueRouter.prototype.push;
const originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function push(location) {
    if (typeof (location) == "string") {
        var Separator = "&";
        if (location.indexOf('?') == -1) { Separator = '?'; }
        location = location + Separator + "random=" + Math.random();
    }
    return routerPush.call(this, location).catch(error => error)
};
VueRouter.prototype.replace= function (location, res, rej) {
    if (res && rej) {
      originReplace.call(this, location, res, rej)
    } else {
      originReplace.call(this,location,() => {},() => {})
    }
}
//全局路由守卫,登陆后不允许跳转至登录界面
router.beforeEach((to, from, next) => {
    //用户登陆了不允许跳登录页
    if (store.state.user.token && to.name == "Login") {
        next(false);//不放行且页面回跳,如果不操作会出bug
        //next('/home');//也可以指定跳转的路径为首页
    }

    //未登录不允许跳转购物车，支付页等

    //其余情况正常跳转
    else {
        //先取到用户信息再跳转
        try {
            store.dispatch('getUserInfo');//每次路由跳转必须要捞数据，否则刷新就没了
            next();
        } catch (error) {
            console.log(error.message);  
        }
    }
})
export default router;